<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Member\Controller;

use User\Api\UserApi as UserApi;
/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends MemberController {

	//显示界面
    public function index(){
    	//判断当前用户是否已经登录
    	if(UID){
    		
    		$usermodules = session('usermodules');
    		$useropenmodules = array();
    		foreach ($this->noallow as $v){
    		    if(in_array($v, $usermodules)){
    		    	$useropenmodules[$v] = 1;
    		    }else{
    		    	$useropenmodules[$v] = 0;
    		    }
    		}
    		$this->assign('uid',UID);
    		$this->assign('useropenmodules',$useropenmodules);
    		$this->display();
    	} else {
    		$this->redirect('User/login');
    	}
    	
    }
    
    /**
     * 显示用户相关信息
     */
    public function userinfo(){
    	
    	$usermodules = session('usermodules');
    	$useropenmodules = array();
    	foreach ($this->noallow as $v){
    		if(in_array($v, $usermodules)){
    			$useropenmodules[$v] = 1;
    		}else{
    			$useropenmodules[$v] = 0;
    		}
    	}
    	$this->assign('useropenmodules',$useropenmodules);
     	//dump($useropenmodules);exit;
    	$memberLogic = D('Member','Logic');
    	$userinfo = $memberLogic->info(UID);
    	$this->assign('userinfo',$userinfo);
    	
    	$AnalyseLogic=D('Analyse','Logic');
    	
    	$websitemap = array(
    			'uid'=>UID,
    			'type'=>'website_view',
    			'time'=>array('gt',strtotime(date('Y-m-d',time()-1*24*3600))),
    	);
    	
    	$useraddmap = array(
    			'uid'=>UID,
    			'type'=>'useradd',
    			'time'=>array('gt',strtotime(date('Y-m-d',time()-1*24*3600))),
    	);
    	
    	$fruitmap = array(
    			'uid'=>UID,
    			'type'=>'fruit_view',
    			'time'=>array('gt',strtotime(date('Y-m-d',time()-1*24*3600))),
    	);
    	
    	$sctatchmap = array(
    			'uid'=>UID,
    			'type'=>'sctatch_view',
    			'time'=>array('gt',strtotime(date('Y-m-d',time()-1*24*3600))),
    	);
    	$turntablemap = array(
    			'uid'=>UID,
    			'type'=>'turntable_view',
    			'time'=>array('gt',strtotime(date('Y-m-d',time()-1*24*3600))),
    	);
    	
    	$ConfigLogic=D('Config','Logic');
    	
    	$map=array(
    			'uid'	=>	UID,
    			'type'	=>	'reserve_list',
    	);
    	
    	$data=$ConfigLogic->where($map)->select();
    	
		$reservelogLogic = D('reservelog','Logic');
		$reservecount=0;
    	foreach ($data as $key => $value){
    		
    		$reservemap=array(
    				'rid'=>$value['id'],
    				'time'=>array('gt',strtotime(date('Y-m-d',time()-1*24*3600)))
    				
    		);
    		
    		$reservecount+=$reservelogLogic->where($reservemap)->count();
    	}
    	
    	$websiteview=$AnalyseLogic->where($websitemap)->find();
    	
    	$useradd=$AnalyseLogic->where($useraddmap)->find();
    	
    	$fruit=$AnalyseLogic->where($fruitmap)->find();
    	
    	$sctatch=$AnalyseLogic->where($sctatchmap)->find();

    	$turntable=$AnalyseLogic->where($turntablemap)->find();
    	
    	$analyse=array(
    			'websiteview'=>$websiteview['count'],
    			'useradd'	=>$useradd['count'],
    			'activity'	=>$fruit['count']+$sctatch['count']+$turntable['count'],
    			'reserve'	=>$reservecount,
    	);
    	
    	$this->assign('analyse',$analyse);
    	
        $this->display();
    }
    
     /**
     * 修改用户信息
     */
    public function changepasswd(){
    	//如果是提交修改密码的信息
    	if(IS_POST){
    		
    		$data = array();
    	   
    	   //获取参数
    	   $password   =   I('post.oldpasswd');
    	   empty($password) && $this->error('请输入原密码');
    	   $data['password'] = I('post.newpasswd');
    	   empty($data['password']) && $this->error('请输入新密码');
    	   $repassword = I('post.confirmpasswd');
    	   empty($repassword) && $this->error('请输入确认密码');
    	   
    	   if($data['password'] !== $repassword){
    	   	    $this->error('您输入的新密码与确认密码不一致');
    	   }
    	   
    	   if(strlen($data['password']) < 6){
    	   	$this->error('新密码的长度小于6位');
    	   }
    	   
//     	   dump($data);exit;
    	   
    	   $Api    =   new UserApi();
    	   $res    =   $Api->updateInfo(UID, $password, $data);
    	   if($res['status']){
    	   		$this->success('修改密码成功！');
    	   }else{
    	   		$this->error($res['info']);
    	   }
    	   
    	   exit;
    	}
    	
    	$this->display();
    }
    
}